﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;

namespace HYView
{
	public partial class ZoomViewToSlectedAreaForm : System.Windows.Forms.Form
	{
		public ZoomViewToSlectedAreaForm(ExternalCommandData cmdData, Autodesk.Revit.DB.View activeView, List<string> viewNames)
		{
			this.InitializeComponent();
			base.Location = RevitVersionFuncs.GetRevitTopLeftPoint(cmdData);
			this.clbViews.Items.Clear();
			foreach (string text in viewNames)
			{
				this.clbViews.Items.Add(text);
				if (activeView.Name == text)
				{
					this.clbViews.SelectedItem = text;
					this.clbViews.SetItemChecked(this.clbViews.Items.Count - 1, true);
				}
			}
		}

		private void ZoomViewToSlectedAreaForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		public List<int> GetSelectViewIndex()
		{
			List<int> list = new List<int>();
			for (int i = 0; i < this.clbViews.Items.Count; i++)
			{
				if (this.clbViews.GetItemChecked(i))
				{
					list.Add(i);
				}
			}
			return list;
		}

		private void ZoomViewToSlectedAreaForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void ZoomViewToSlectedAreaForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			ZoomViewToSlectedAreaForm.SendMessage(CmdZoomViewToSlectedArea._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			ZoomViewToSlectedAreaForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			ZoomViewToSlectedAreaForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}
	}
}
